package com.awonsome.mall.ums.boot.controller;

import cn.hutool.core.util.StrUtil;
import com.awonsome.common.core.result.Result;
import com.awonsome.mall.ums.boot.service.IUmsMemberService;
import com.awonsome.mall.ums.pojo.entity.UmsMember;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

import static com.awonsome.common.core.constant.GlobalConstants.STATUS_YES;

/**
 * @author yangdejun
 * @date 2022/1/29 20:42
 */
@AllArgsConstructor
@RestController
@RequestMapping(value = "/api/v1/users")
public class MemberController {

    private final IUmsMemberService iUmsMemberService;

    /**
     * 用户列表
     *
     * @param page 页码
     * @param limit 每页数量
     * @param nickName 会员昵称
     * @return
     */
    @GetMapping(value = "/")
    public Result<List<UmsMember>> list(Integer page, Integer limit, String nickName) {
        LambdaQueryWrapper<UmsMember> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.ne(UmsMember::getDeleted, STATUS_YES);
        queryWrapper.like(StrUtil.isNotBlank(nickName), UmsMember::getNickName, nickName);
        IPage<UmsMember> result = iUmsMemberService.list(new Page<>(page, limit), nickName);
        return Result.success(result.getRecords(), result.getTotal());
    }

    /**
     * 根据会员 ID 查询会员详情
     *
     * @param id 会员 ID
     * @return
     */
    @GetMapping(value = "/{id}")
    public Result<UmsMember> getMemberById(@PathVariable Long id) {
        UmsMember user = iUmsMemberService.getById(id);
        return Result.success(user);
    }

    /**
     * 根据会员 ID 更新会员信息
     *
     * @param id 会员 ID
     * @param member 会员信息
     * @param <T>
     * @return
     */
    @PutMapping(value = "/{id}")
    public <T> Result<T> update(@PathVariable Long id, @RequestBody UmsMember member) {
        boolean status = iUmsMemberService.updateById(member);
        return Result.judge(status);
    }

    /**
     * 选择性更新会员信息
     *
     * @param id 用户 ID
     * @param user 用户信息
     * @param <T>
     * @return
     */
    @PatchMapping(value = "/{id}")
    public <T> Result<T> patch(@PathVariable Long id, @RequestBody UmsMember user) {
        LambdaUpdateWrapper<UmsMember> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.set(user.getStatus() != null, UmsMember::getStatus, user.getStatus());
        boolean status = iUmsMemberService.update(updateWrapper);
        return Result.judge(status);
    }

    /**
     * 根据会员 ID 批量删除会员
     *
     * @param ids 逗号分隔的会员 ID 数组
     * @param <T>
     * @return
     */
    @DeleteMapping(value = "/{ids}")
    public <T> Result<T> delete(@PathVariable String ids) {
        boolean status = iUmsMemberService.update(new LambdaUpdateWrapper<UmsMember>()
                .in(UmsMember::getId, Arrays.asList(ids.split(",")))
                .set(UmsMember::getDeleted, STATUS_YES));
        return Result.judge(status);
    }

}
